package com.atguigu.gmall.realtime.demo;


import com.alibaba.fastjson.JSONObject;
import org.apache.doris.flink.cfg.DorisExecutionOptions;
import org.apache.doris.flink.cfg.DorisOptions;
import org.apache.doris.flink.cfg.DorisSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Properties;

/**
 * @author caodan
 * @version 1.0
 * @description
 * @date 2025-08-01 16:33
 */
public class FlinkDorisWriteDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        Properties pro = new Properties();
        pro.setProperty("format", "json");
        pro.setProperty("strip_outer_array", "true");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", 3);
        jsonObject.put("code", "1002");
        env
                .fromElements(jsonObject.toString())
                .addSink(DorisSink.sink(
                        new DorisExecutionOptions.Builder()
//                                .setBatchIntervalMs(2000L)
                                .setEnableDelete(false)
                                .setMaxRetries(3)
                                .setStreamLoadProp(pro)
                                .build(),
                        new DorisOptions.Builder()
                                .setFenodes("192.168.10.102:8030")
                                .setUsername("root")
                                .setPassword("Caodan520@")
                                .setTableIdentifier("doris_test.hello")
                                .build())
                );

        env.execute();
    }
}
